package com.milhouz.bomberrokon;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.milhouz.bomberrokon.anim.GalleryLayout;
import com.milhouz.bomberrokon.anim.HorizontalSlidingLayoutWithAnim;
import com.milhouz.bomberrokon.factories.BombersFactory;
import com.milhouz.bomberrokon.model.MapModel;
import com.milhouz.bomberrokon.util.BomberXmlReader;
import com.milhouz.bomberrokon.util.MapXmlReader;

public class GameSettingsActivity extends Activity {

	private Animation mButtonFadeAnimation;

	private View mPlayButton;
	private EditText mRoundsEditText;
	private View mPlusRoundsButton;
	private View mMinusRoundsButton;
	private EditText mOpponentsEditText;
	private View mOpponentsPlusButton;
	private View mOpponentsMinusButton;
	private EditText mTimerEditText;
	private View mTimerPlusButton;
	private View mTimerMinusButton;

	private View mTitleRounds;
	private View mTitleBots;
	private View mTitleTimer;

	private String mSelectedMapCode;
	private int mSelectedMapTileSize;

	private HorizontalSlidingLayoutWithAnim mBomberLayoutGallery;

	private final int MAX_TIMER = 999;
	private final int MIN_TIMER = 30;
	private int mMaxOpponents = 3;
	private final int MIN_OPPONENTS = 1;
	private final int MIN_ROUNDS = 1;
	private final int MAX_ROUNDS = 20;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set the fullscreen mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.game_settings);

		// Get UI components
		mPlayButton = findViewById(R.id.playButton);
		mPlusRoundsButton = findViewById(R.id.roundsPlusButton);
		mMinusRoundsButton = findViewById(R.id.roundsMinusButton);
		mRoundsEditText = (EditText) findViewById(R.id.roundsEditText);
		mOpponentsEditText = (EditText) findViewById(R.id.opponentsEditText);
		mOpponentsPlusButton = findViewById(R.id.opponentsPlusButton);
		mOpponentsMinusButton = findViewById(R.id.opponentsMinusButton);
		mTimerEditText = (EditText) findViewById(R.id.timerEditText);
		mTimerPlusButton = findViewById(R.id.timerPlusButton);
		mTimerMinusButton = findViewById(R.id.timerMinusButton);
		mTitleRounds = findViewById(R.id.title_rounds);
		mTitleBots = findViewById(R.id.title_opponents);
		mTitleTimer = findViewById(R.id.title_timer);

		// Add buttons listener
		mPlayButton.setOnClickListener(sPlayButtonListener);
		mPlusRoundsButton.setOnClickListener(sRoundsPlusButtonListener);
		mMinusRoundsButton.setOnClickListener(sRoundsMinusButtonListener);
		mOpponentsPlusButton.setOnClickListener(sOpponentsPlusButtonListener);
		mOpponentsMinusButton.setOnClickListener(sOpponentsMinusButtonListener);
		mTimerPlusButton.setOnClickListener(sTimerPlusButtonListener);
		mTimerMinusButton.setOnClickListener(sTimerMinusButtonListener);
		mButtonFadeAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);

		// Get selected Map code
		final Intent callingIntent = this.getIntent();
		mSelectedMapCode = callingIntent.getStringExtra("mapCode");

		loadMapData();
		loadBombersDataForSlider();
	}

	private View.OnClickListener sPlayButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Intent i = new Intent(getBaseContext(), BomberSlashActivity.class);
			i.putExtra("mapCode", mSelectedMapCode);
			i.putExtra("bomberCode", mBomberLayoutGallery.getCurrentImgCode());
			i.putExtra("opponents", Integer.valueOf(mOpponentsEditText.getText().toString()));
			i.putExtra("timer", Integer.valueOf(mTimerEditText.getText().toString()));
			i.putExtra("nbRounds", Integer.valueOf(mRoundsEditText.getText().toString()));
			v.startAnimation(mButtonFadeAnimation);

			mRoundsEditText.startAnimation(mButtonFadeAnimation);
			mPlusRoundsButton.startAnimation(mButtonFadeAnimation);
			mMinusRoundsButton.startAnimation(mButtonFadeAnimation);
			mOpponentsEditText.startAnimation(mButtonFadeAnimation);
			mOpponentsPlusButton.startAnimation(mButtonFadeAnimation);
			mOpponentsMinusButton.startAnimation(mButtonFadeAnimation);
			mTimerEditText.startAnimation(mButtonFadeAnimation);
			mTimerPlusButton.startAnimation(mButtonFadeAnimation);
			mTimerMinusButton.startAnimation(mButtonFadeAnimation);
			mTitleRounds.startAnimation(mButtonFadeAnimation);
			mTitleBots.startAnimation(mButtonFadeAnimation);
			mTitleTimer.startAnimation(mButtonFadeAnimation);

			mButtonFadeAnimation.setAnimationListener(new StartActivityAfterAnimation(i));
		}
	};

	private View.OnClickListener sRoundsPlusButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Integer value = Integer.valueOf(mRoundsEditText.getText().toString());
			if (value < MAX_ROUNDS) {
				value++;
				mRoundsEditText.setText(String.valueOf(value));
			}
		}
	};

	private View.OnClickListener sRoundsMinusButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Integer value = Integer.valueOf(mRoundsEditText.getText().toString());
			if (value > MIN_ROUNDS) {
				value--;
				mRoundsEditText.setText(String.valueOf(value));
			}
		}
	};

	private View.OnClickListener sOpponentsPlusButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Integer value = Integer.valueOf(mOpponentsEditText.getText().toString());
			if (value < mMaxOpponents) {
				value++;
				mOpponentsEditText.setText(String.valueOf(value));
			}
		}
	};

	private View.OnClickListener sOpponentsMinusButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Integer value = Integer.valueOf(mOpponentsEditText.getText().toString());
			if (value > MIN_OPPONENTS) {
				value--;
				mOpponentsEditText.setText(String.valueOf(value));
			}
		}
	};

	private View.OnClickListener sTimerPlusButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Integer value = Integer.valueOf(mTimerEditText.getText().toString());
			if (value < MAX_TIMER) {
				value += 5;
				mTimerEditText.setText(String.valueOf(value));
			}
		}
	};

	private View.OnClickListener sTimerMinusButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Integer value = Integer.valueOf(mTimerEditText.getText().toString());
			if (value > MIN_TIMER) {
				value -= 5;
				mTimerEditText.setText(String.valueOf(value));
			}
		}
	};

	private void loadMapData() {
		// Get and load map meta data
		MapModel mapModel = MapXmlReader.getMapSettings(this, mSelectedMapCode);
		mMaxOpponents = mapModel.getMaxPlayers() - 1;
		mOpponentsEditText.setText(String.valueOf(mMaxOpponents));
		mSelectedMapTileSize = mapModel.getTileSize();
	}

	private void loadBombersDataForSlider() {
		String tilePath = mSelectedMapTileSize + "x" + mSelectedMapTileSize;
		// Load default Map Settings
		String[] bombers = BombersFactory.getFactory().listBombers(getAssets(), mSelectedMapTileSize);

		// Get the slider and add views
		mBomberLayoutGallery = (HorizontalSlidingLayoutWithAnim) findViewById(R.id.layoutBomberGallery);
		mBomberLayoutGallery.MAX_CHILDREN = bombers.length;
		mBomberLayoutGallery.SPACE_BETWEEN_VIEW = 50;
		if (bombers.length % 2 == 1) {
			mBomberLayoutGallery.NUMBER_CHILDREN_UNDER = Math.round(bombers.length / 2) - 1;
		} else {
			mBomberLayoutGallery.NUMBER_CHILDREN_UNDER = bombers.length / 2;
		}

		ImageView map = null;
		GalleryLayout linearLayout = null;
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( //
				LinearLayout.LayoutParams.WRAP_CONTENT, //
				LinearLayout.LayoutParams.WRAP_CONTENT);

		for (String bomberCode : bombers) {
			linearLayout = new GalleryLayout(bomberCode, getApplicationContext());
			linearLayout.setPadding(5, 5, 5, 5);
			linearLayout.setBackgroundColor(Color.WHITE);
			map = new ImageView(getApplicationContext());
			try {
				map.setBackgroundDrawable(Drawable.createFromStream(getAssets().open(BomberXmlReader.ASSETS_IMG_FOLDER + "/" + tilePath + //
						"/" + BomberXmlReader.ASSETS_BOMBER_FOLDER + //
						"/" + bomberCode + //
						"/" + BomberXmlReader.THUMB_FILENAME), bomberCode));
			} catch (IOException e) {
				map.setBackgroundResource(R.drawable.default_bomber_thumb);
			}
			linearLayout.addView(map);
			mBomberLayoutGallery.addView(linearLayout, layoutParams);
		}
	}

	protected class StartActivityAfterAnimation implements Animation.AnimationListener {
		private Intent mIntent;

		StartActivityAfterAnimation(Intent intent) {
			mIntent = intent;
		}

		public void onAnimationEnd(Animation animation) {
			mPlayButton.setVisibility(View.INVISIBLE);
			mPlayButton.clearAnimation();
			startActivity(mIntent);
		}

		public void onAnimationRepeat(Animation animation) {

		}

		public void onAnimationStart(Animation animation) {

		}

	}

}
